﻿Imports System.Data
Imports DevExpress.XtraGrid.Columns
Imports DevExpress.XtraGrid.Views.Grid
Imports DevExpress.XtraGrid
Imports DevExpress.XtraEditors
Imports DevExpress.XtraSplashScreen

Public Class Thu
    Private state As Integer '0 default 1 tao moi 2 sua
    Private Sub setDefault()
        Me.NghiepvuTextEdit.EditValue = "Thu"
        Me.TaikhoannoTextEdit.EditValue = "331"
    End Sub
    Private Sub setReadonly(ByVal b As Boolean)
        Me.MaTextEdit.Properties.ReadOnly = b
        Me.SochungtuTextEdit.Properties.ReadOnly = b
        Me.SearchLookUpEdit1.Properties.ReadOnly = b
        Me.NguoinopTextEdit.Properties.ReadOnly = b
        Me.NgayDateEdit.Properties.ReadOnly = b
        Me.TongtienTextEdit.Properties.ReadOnly = b
        If b Then
            Me.IGridView1.OptionsBehavior.Editable = False 'không cho phép chỉnh sửa trên lưới 
            Me.IGridView1.OptionsView.NewItemRowPosition = NewItemRowPosition.None 'không cho thêm dòng trống dưới cùng
        Else
            Me.IGridView1.OptionsBehavior.Editable = True 'có cho phép chỉnh sửa trên lưới 
            Me.IGridView1.OptionsView.NewItemRowPosition = NewItemRowPosition.Bottom ' cho thêm dòng trống dưới cùng
        End If
    End Sub
    Private Sub choPhepButtonChiTiet(ByVal b As Boolean)
        Me.BarButtonItemTaoMoiChiTiet.Enabled = b
        Me.BarButtonItemXoaChiTiet.Enabled = b
        Me.BarButtonItemHuyThayDoiChiTiet.Enabled = b
    End Sub
    Private Sub choPhepButtonLuuXoa(ByVal b As Boolean)
        Me.BarButtonItemLuu.Enabled = b
        Me.BarButtonItemXoa.Enabled = b
        If b Then
            Me.BarButtonItemSua.Enabled = False
        Else
            Me.BarButtonItemSua.Enabled = True
        End If
    End Sub
    Private Sub BarButtonItemThoat_ItemClick(sender As System.Object, e As DevExpress.XtraBars.ItemClickEventArgs) Handles BarButtonItemThoat.ItemClick
        Me.Close()
    End Sub

    Private Sub BarButtonItemTaoMoi_ItemClick(sender As System.Object, e As DevExpress.XtraBars.ItemClickEventArgs) Handles BarButtonItemTaoMoi.ItemClick
        Try
            Me.setReadonly(False)
            Me.choPhepButtonLuuXoa(True)
            Me.choPhepButtonChiTiet(True)
            Me.MaTextEdit.Focus()
            Me.ThuBindingSource.AddNew()
            Me.state = 1
            setDefault()
        Catch ex As Exception
        End Try
    End Sub

    Private Sub Frm_DonMau_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
        SplashScreenManager.ShowForm(GetType(WaitForm1))
        Me.KhachhangTableAdapter.Fill(Me.PcvgppDataSet.khachhang)
        Me.ThuTableAdapter.Fill(Me.PcvgppDataSet.thu)
        Me.ThuchitietTableAdapter.Fill(Me.PcvgppDataSet.thuchitiet)
        setReadonly(True)
        Me.IGridView1.BestFitColumns()
        Me.choPhepButtonChiTiet(False)
        Me.choPhepButtonLuuXoa(False)
        Me.ThuBindingSource.MoveLast()
        Me.state = 0
        SplashScreenManager.CloseForm()
    End Sub

    Private Sub BarButtonItemLuu_ItemClick(sender As System.Object, e As DevExpress.XtraBars.ItemClickEventArgs) Handles BarButtonItemLuu.ItemClick
        Try
            Me.IGridView1.CloseEditor()
            Me.ThuBindingSource.EndEdit()
            Me.FKthuchitietthuBindingSource.EndEdit()
            If Me.PcvgppDataSet.HasChanges Then
                If DevExpress.XtraEditors.XtraMessageBox.Show("Bạn có muốn 'Lưu' không ?", "Thông báo", MessageBoxButtons.YesNo, MessageBoxIcon.Information) = Windows.Forms.DialogResult.Yes Then
                    Me.setTongTien()
                    Me.ThuBindingSource.EndEdit()
                    Me.ThuTableAdapter.Update(Me.PcvgppDataSet.thu)
                    Me.ThuchitietTableAdapter.Update(Me.PcvgppDataSet.thuchitiet)
                    Me.state = 0
                    Me.setReadonly(True)
                    Me.choPhepButtonLuuXoa(False)
                    Me.choPhepButtonChiTiet(False)
                End If
            Else
                DevExpress.XtraEditors.XtraMessageBox.Show("Chưa có thay đổi dữ liệu ?", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Information)
                Me.state = False
                Me.setReadonly(True)
                Me.choPhepButtonLuuXoa(False)
                Me.choPhepButtonChiTiet(False)
            End If
        Catch ex As Exception
            MessageBox.Show(ex.Message)
        End Try
    End Sub
    Private Function kiemTraMaTonTaiChua() As Boolean 'đảm bảo mã là duy nhất 
        For index = 0 To Me.IGridView1.RowCount - 1
            If Me.IGridView1.GetRowCellValue(index, "ma") = Me.MaTextEdit.EditValue Then
                Return True
                Exit Function
            End If
        Next
        Return False
    End Function

    Private Sub MaTextEdit_Validating(sender As Object, e As System.ComponentModel.CancelEventArgs)
        If kiemTraMaTonTaiChua() And state Then
            Me.MaTextEdit.ErrorText = "Mã đã tồn tại"
            e.Cancel = True
        End If
    End Sub

    Private Sub BarButtonItemSua_ItemClick(sender As System.Object, e As DevExpress.XtraBars.ItemClickEventArgs) Handles BarButtonItemSua.ItemClick
        Me.setReadonly(False)
        Me.choPhepButtonLuuXoa(True)
        Me.choPhepButtonChiTiet(True)
        Me.state = 2
    End Sub

    Private Sub BarButtonItemFirst_ItemClick(sender As System.Object, e As DevExpress.XtraBars.ItemClickEventArgs) Handles BarButtonItemFirst.ItemClick
        Me.ThuBindingSource.MoveFirst()
    End Sub

    Private Sub BarButtonItem2_ItemClick(sender As System.Object, e As DevExpress.XtraBars.ItemClickEventArgs) Handles BarButtonItem2.ItemClick
        Me.ThuBindingSource.MovePrevious()
    End Sub

    Private Sub BarButtonItem6_ItemClick(sender As System.Object, e As DevExpress.XtraBars.ItemClickEventArgs) Handles BarButtonItem6.ItemClick
        Me.ThuBindingSource.MoveNext()
    End Sub

    Private Sub BarButtonItem7_ItemClick(sender As System.Object, e As DevExpress.XtraBars.ItemClickEventArgs) Handles BarButtonItem7.ItemClick
        Me.ThuBindingSource.MoveLast()
    End Sub

    Private Sub BarButtonItemXoa_ItemClick(sender As System.Object, e As DevExpress.XtraBars.ItemClickEventArgs) Handles BarButtonItemXoa.ItemClick
        If DevExpress.XtraEditors.XtraMessageBox.Show("Bạn có muốn 'Xóa' không ?", "Thông báo", MessageBoxButtons.YesNo, MessageBoxIcon.Information) = Windows.Forms.DialogResult.Yes Then
            Me.ThuBindingSource.RemoveCurrent()
            Me.ThuTableAdapter.Update(Me.PcvgppDataSet.thu)
            Me.state = False
            Me.setReadonly(True)
            Me.choPhepButtonLuuXoa(False)
            Me.choPhepButtonChiTiet(False)
        End If
    End Sub

    Private Sub BarButtonItemHuyThayDoi_ItemClick(sender As System.Object, e As DevExpress.XtraBars.ItemClickEventArgs) Handles BarButtonItemHuyThayDoi.ItemClick
        Me.ThuBindingSource.EndEdit()
        If Me.PcvgppDataSet.HasChanges Then
            If DevExpress.XtraEditors.XtraMessageBox.Show("Bạn có muốn hủy tất cả những thay đổi đang làm không ?", "Thông báo", MessageBoxButtons.YesNo, MessageBoxIcon.Information) = Windows.Forms.DialogResult.Yes Then
                Me.PcvgppDataSet.RejectChanges()
            End If
        Else
            DevExpress.XtraEditors.XtraMessageBox.Show("Chưa có thay đổi dữ liệu ?", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Information)
        End If
    End Sub
    Private Sub batFocusGrid()
        '========== bắt focus trên lưới ===========
        IGridView1.FocusedRowHandle = 4
        IGridView1.FocusedColumn = IGridView1.VisibleColumns(0)
        IGridView1.ShowEditor()
        '============================
    End Sub

    Private Sub DonviTextEdit_GotFocus(ByVal sender As Object, ByVal e As System.EventArgs)
        Me.SearchLookUpEdit1.ShowPopup()
    End Sub
    Private Sub DonviTextEdit_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs)
        If (e.KeyCode = Keys.Enter Or e.KeyCode = Keys.Tab) And (state = 1 Or state = 2) Then
            'IGridView1.AddNewRow() 'thêm dòng mới
            'Me.IGridView1.SetFocusedRowCellValue("ma", Me.MaTextEdit.EditValue)
            'Me.IGridView1.SetFocusedRowCellValue("taikhoanco", "111")
            'batFocusGrid()
            IGridView1.AddNewRow() 'thêm dòng mới
            'Me.IGridView1.SetFocusedRowCellValue("ma", Me.MaTextEdit.EditValue)
            'Me.IGridView1.SetFocusedRowCellValue("tenhanghoa", "")
            'batFocusGrid()
            Dim col As GridColumn = IGridView1.Columns("ma")
            Me.IGridView1.FocusedColumn = col
            Me.IGridView1.SetFocusedRowCellValue("ma", Me.MaTextEdit.EditValue)
            IGridView1.ShowEditor()

        End If
    End Sub
    Private Sub setTongTien()
        Me.IGridView1.UpdateCurrentRow()
        Me.IGridView1.UpdateSummary()
        Me.TongtienTextEdit.EditValue = Me.IGridView1.Columns("phatsinh").SummaryItem.SummaryValue
    End Sub
    Private Sub IGridView1_CellValueChanged(sender As Object, e As DevExpress.XtraGrid.Views.Base.CellValueChangedEventArgs) Handles IGridView1.CellValueChanged
        'setTongTien()
        Dim view As GridView = CType(sender, GridView)
        If view.UpdateCurrentRow() Then
            view.UpdateSummary()
            Me.TongtienTextEdit.EditValue = Me.IGridView1.Columns("phatsinh").SummaryItem.SummaryValue
        End If
    End Sub

    Private Sub IGridView1_InvalidRowException(sender As Object, e As DevExpress.XtraGrid.Views.Base.InvalidRowExceptionEventArgs) Handles IGridView1.InvalidRowException
        e.ExceptionMode = DevExpress.XtraEditors.Controls.ExceptionMode.NoAction
    End Sub

    Private Sub IGridView1_KeyDown(sender As Object, e As System.Windows.Forms.KeyEventArgs) Handles IGridView1.KeyDown
        If Me.IGridView1.FocusedColumn.Name = "colsochungtu" Then
            setTongTien()
        End If
    End Sub

    Private Sub IGridView1_RowUpdated(sender As Object, e As DevExpress.XtraGrid.Views.Base.RowObjectEventArgs) Handles IGridView1.RowUpdated
        setTongTien()
    End Sub

    Private Sub IGridView1_ShownEditor(sender As Object, e As System.EventArgs) Handles IGridView1.ShownEditor
        Dim view As GridView = CType(sender, GridView)
        If TypeOf view.ActiveEditor Is SearchLookUpEdit And IsDBNull(Me.IGridView1.FocusedValue) Then
            DirectCast(view.ActiveEditor, SearchLookUpEdit).ShowPopup()
        End If
    End Sub

    Private Sub IGridView1_ValidatingEditor(sender As Object, e As DevExpress.XtraEditors.Controls.BaseContainerValidateEditorEventArgs) Handles IGridView1.ValidatingEditor
        Dim View As GridView = sender
        If View.FocusedColumn.FieldName = "phatsinh" Then
            Dim phatsinh As Double
            Try
                phatsinh = Convert.ToDouble(e.Value)
            Catch ex As Exception

            End Try
            If phatsinh = 0 Then
                e.Valid = False
                e.ErrorText = "Không thể bằng 0"
            End If
        End If
    End Sub

    Private Sub BarButtonItem1_ItemClick(sender As System.Object, e As DevExpress.XtraBars.ItemClickEventArgs) Handles BarButtonItemTaoMoiChiTiet.ItemClick
        IGridView1.Focus()
        IGridView1.AddNewRow()
        batFocusGrid()
    End Sub

    Private Sub BarButtonItem10_ItemClick(sender As System.Object, e As DevExpress.XtraBars.ItemClickEventArgs) Handles BarButtonItemXoaChiTiet.ItemClick
        If DevExpress.XtraEditors.XtraMessageBox.Show("Bạn có muốn xóa dòng đang chọn không ?", "Thông báo", MessageBoxButtons.YesNo, MessageBoxIcon.Stop) = Windows.Forms.DialogResult.Yes Then
            Dim view As GridView = CType(IGridView1, GridView)
            view.DeleteRow(view.FocusedRowHandle)
            Me.setTongTien()
        End If
    End Sub

    Private Sub BarButtonItemFirstChiTiet_ItemClick(sender As System.Object, e As DevExpress.XtraBars.ItemClickEventArgs) Handles BarButtonItemFirstChiTiet.ItemClick
        Me.FKthuchitietthuBindingSource.MoveFirst()
    End Sub

    Private Sub BarButtonItemPreviousChiTiet_ItemClick(sender As System.Object, e As DevExpress.XtraBars.ItemClickEventArgs) Handles BarButtonItemPreviousChiTiet.ItemClick
        Me.FKthuchitietthuBindingSource.MovePrevious()
    End Sub

    Private Sub BarButtonItemNextChiTiet_ItemClick(sender As System.Object, e As DevExpress.XtraBars.ItemClickEventArgs) Handles BarButtonItemNextChiTiet.ItemClick
        Me.FKthuchitietthuBindingSource.MoveNext()
    End Sub

    Private Sub BarButtonItemLastChiTiet_ItemClick(sender As System.Object, e As DevExpress.XtraBars.ItemClickEventArgs) Handles BarButtonItemLastChiTiet.ItemClick
        Me.FKthuchitietthuBindingSource.MoveLast()
    End Sub

    Private Sub BarButtonItemHuyThayDoiChiTiet_ItemClick(sender As System.Object, e As DevExpress.XtraBars.ItemClickEventArgs) Handles BarButtonItemHuyThayDoiChiTiet.ItemClick
        Me.IGridView1.CloseEditor()
        Me.FKthuchitietthuBindingSource.EndEdit()
        If Me.PcvgppDataSet.HasChanges Then
            If DevExpress.XtraEditors.XtraMessageBox.Show("Bạn có muốn hủy tất cả những thay đổi đang làm không ?", "Thông báo", MessageBoxButtons.YesNo, MessageBoxIcon.Information) = Windows.Forms.DialogResult.Yes Then
                Me.PcvgppDataSet.RejectChanges()
            End If
        Else
            DevExpress.XtraEditors.XtraMessageBox.Show("Chưa có thay đổi dữ liệu ?", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Information)
        End If
    End Sub
    Private Sub IGridView3_FocusedRowChanged(sender As Object, e As DevExpress.XtraGrid.Views.Base.FocusedRowChangedEventArgs) Handles IGridView3.FocusedRowChanged
        Try
            ' MessageBox.Show(Me.IGridView3.GetRowCellValue(Me.IGridView3.FocusedRowHandle, "ma"))
            ' MessageBox.Show(Me.IGridView3.GetGroupRowValue(Me.IGridView3.FocusedRowHandle))
            Me.ThuBindingSource.Position = Me.ThuBindingSource.Find("ma", Me.IGridView3.GetGroupRowValue(Me.IGridView3.FocusedRowHandle))
        Catch ex As Exception

        End Try
    End Sub

    Private Sub NgayDateEdit_GotFocus(sender As Object, e As System.EventArgs) Handles NgayDateEdit.GotFocus
        Me.NgayDateEdit.ShowPopup()
    End Sub

    Private Sub BarButtonItemIn_ItemClick(sender As System.Object, e As DevExpress.XtraBars.ItemClickEventArgs) Handles BarButtonItemIn.ItemClick
        Dim report As XtraReport_Phieuthu = New XtraReport_Phieuthu
        report.setSophieu("Số phiếu : " & Me.MaTextEdit.Text)
        report.setDonvi(Me.SearchLookUpEdit1.Text)
        report.setTongtien(CDbl(Me.TongtienTextEdit.Text))
        report.setNgaythang("Ngày " & Me.NgayDateEdit.Text.Substring(0, 2) & " tháng " & Me.NgayDateEdit.Text.Substring(3, 2) & " năm " & Me.NgayDateEdit.Text.Substring(6, 4))
        report.setDiaChiPhieu(Me.DiachiTextEdit.EditValue)
        report.ShowPreview()
    End Sub



    Private Sub SearchLookUpEdit1_EditValueChanging(ByVal sender As Object, ByVal e As DevExpress.XtraEditors.Controls.ChangingEventArgs) Handles SearchLookUpEdit1.EditValueChanging
        Dim rowHandle As Integer = SearchLookUpEdit1.Properties.GetIndexByKeyValue(e.NewValue)
        Me.DiachiTextEdit.EditValue = SearchLookUpEdit1.Properties.View.GetRowCellValue(rowHandle, "diachi")
    End Sub

    Private Sub SearchLookUpEdit1_GotFocus(ByVal sender As Object, ByVal e As System.EventArgs) Handles SearchLookUpEdit1.GotFocus
        Me.SearchLookUpEdit1.ShowPopup()

    End Sub

    Private Sub SearchLookUpEdit1_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles SearchLookUpEdit1.KeyDown
        'truong hop tao moi , state = 1, gan tong tien = 0, va endedit de ko bi an
        If state = 1 Then
            Me.TongtienTextEdit.EditValue = 0
            Me.ThuBindingSource.EndEdit()
            IGridView1.AddNewRow()
            Me.IGridView1.SetFocusedValue(Me.MaTextEdit.EditValue)
            IGridView1.FocusedRowHandle = 4
            IGridView1.FocusedColumn = IGridView1.VisibleColumns(0)
            IGridView1.ShowEditor()
        End If
        'truong hop sua, state = 2,de nguyen
        If state = 2 Then
            IGridView1.AddNewRow()
            Me.IGridView1.SetFocusedValue(Me.MaTextEdit.EditValue)
            IGridView1.FocusedRowHandle = 4
            IGridView1.FocusedColumn = IGridView1.VisibleColumns(0)
            IGridView1.ShowEditor()
        End If

    End Sub

    Private Sub MaTextEdit_Validating1(ByVal sender As Object, ByVal e As System.ComponentModel.CancelEventArgs) Handles MaTextEdit.Validating
        'kiem tra ma trung: ma tao moi ko dc trung voi cac ma da co
        If Me.ThuBindingSource.Find("ma", Me.MaTextEdit.EditValue) <> -1 Then
            DevExpress.XtraEditors.XtraMessageBox.Show("Ma da ton tai", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Error)
        End If
    End Sub
End Class